package com.example.commuity


import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.activityViewModels
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.common.RouterPath
import com.example.common.base.BaseFragment
import com.example.commuity.adapter.CommunityPageAdapter
import com.example.commuity.attention.AttentionFragment
import com.example.commuity.databinding.FragmentCommuityBinding
import com.example.commuity.recommend.RecommendFragment
import com.example.commuity.viewmodel.CommunityViewModel
import dagger.hilt.android.AndroidEntryPoint


@Route(path = RouterPath.PATH_FRAGMENT_COMMUNITY)
@AndroidEntryPoint
class CommunityFragment : BaseFragment<FragmentCommuityBinding>() {



    private var fragments = arrayListOf<Fragment>()
    private lateinit var mPageAdapter: CommunityPageAdapter

    override fun getLayoutId(): Int=R.layout.fragment_commuity

    override fun initViews() {

        fragments.clear()
        fragments.add(RecommendFragment.newInstance())
        fragments.add(AttentionFragment.newInstance())
        mDataBinding.vpCommunityContent.offscreenPageLimit=1
        mPageAdapter=CommunityPageAdapter(childFragmentManager,fragments,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
        mDataBinding.vpCommunityContent.adapter=mPageAdapter

        mDataBinding.vpCommunityContent.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
               when(position){
                   0->(fragments[position] as RecommendFragment).lazyLoadData()
                   1->(fragments[position] as AttentionFragment).lazyLoadData()
               }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })


        mDataBinding.tabLayout.setupWithViewPager(mDataBinding.vpCommunityContent)



    }

}